package com.liukai.user.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.liukai.novel_base.base.BaseActivity;
import com.liukai.novel_commons.Result;
import com.liukai.novel_commons.db.DBHelper;
import com.liukai.novel_commons.db.UserInfoService;
import com.liukai.novel_commons.entities.UserInfo;
import com.liukai.novel_commons.utils.Constans;
import com.liukai.novel_commons.utils.DBManager;
import com.liukai.novel_commons.utils.JSONUtil;
import com.liukai.novel_commons.utils.LogUtils;
import com.liukai.novel_commons.utils.MyInfoList;
import com.liukai.user.R;
import com.liukai.user.R2;
import com.liukai.user.call_back.IUserCallBack;
import com.liukai.user.presenter.impl.UserPresenterImpl;

import butterknife.BindView;

public class LoginActivity extends BaseActivity implements IUserCallBack {

    @BindView(R2.id.btn_phone_login)
    AppCompatButton btnPhoneLogin;

    @BindView(R2.id.btn_back)
    TextView btnBack;

    private UserPresenterImpl mUserPresenter;

    private Context mContext;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initData(Context context) {
        mUserPresenter = new UserPresenterImpl();
        mUserPresenter.registerCallback(this);
        this.mContext = context;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {

        btnPhoneLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.d(getClass(), "点击本机号码一键登录按钮");
                UserInfo userInfo = new UserInfo();
                userInfo.setUserId("111")
                        .setUserPass("111");
                mUserPresenter.login(userInfo);
            }
        });

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    /**
     * 登录结果返回
     *
     * @param result
     */
    @Override
    public void loginResult(Result result) {
        LogUtils.d(getClass(), result.toString());
        if (result.getCode() != 200) {
            return;
        }
        // 登陆成功，修改设置列表的数据项
        MyInfoList.addSettingList(Constans.LOGOUT);
        UserInfo userInfo = JSONUtil.resolverResult(result, UserInfo.class);
        Constans.setUserInfo(userInfo);
        Intent intent = new Intent();
        intent.putExtra("userInfo", userInfo);
        // 携带登录返回的数据返回我的信息页面
        this.setResult(Constans.CODE_RESULT_LoginActivity, intent);
        LoginActivity.this.finish();

        updateLocalDB(userInfo);
    }

    /**
     * 修改本地数据库
     *
     * @param userInfo
     */
    private void updateLocalDB(UserInfo userInfo) {
        LogUtils.e(getClass(),mContext.toString());
        UserInfoService userInfoService = new UserInfoService(mContext);
        userInfoService.insertUserInfo(userInfo);
    }

}
